package com.eagle.platform.api.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eagle.common.entity.po.AdminUser;
import com.eagle.common.entity.po.query.AdminUserQuery;
import com.eagle.common.exception.BusinessException;
import com.eagle.common.service.AdminUserService;
import com.eagle.common.util.JacksonUtils;
import com.eagle.platform.api.constant.APIResultCodeEnum;

/**
 * 
 * 后台用户表 控制层
 * 
 */
@Controller
@RequestMapping("adminUser")
public class AdminUserController extends BaseController{

	private static final Logger LOGGER = LoggerFactory.getLogger(AdminUserController.class);
	
	@Resource
	private AdminUserService adminUserService;
	
	/**
	 * 查询所有用户或者单个用户信息
	 * param id (用户主键)
	 */
	@RequestMapping(value = "list", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String list(@RequestBody(required = false) String id) {
		List<AdminUser> adminUser=null;
		AdminUserQuery query = null;
		if(!StringUtils.isAllBlank(id)) {
			query=JacksonUtils.fromJson(id, AdminUserQuery.class);
		}
		adminUser=adminUserService.findListByParam(query);
		if (query == null){
            query = new AdminUserQuery();
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, adminUser));
	}
	
	/**
	 * 添加用户信息
	 * param adminUser (用户表)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "insert", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String insert(@RequestBody(required = false) AdminUser adminUser) throws BusinessException {
		Integer query = null;
		System.out.println(adminUser);
		if(adminUser!=null) {
			query=adminUserService.add(adminUser);
		}
		if (query == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
	
	/**
	 * 修改用户信息
	 * param adminUser (用户表)
	 * param id (用户表主键)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "updateById", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String updateById(@RequestBody(required = false) AdminUser adminUser) throws BusinessException {
		Integer query = null;
		Integer id=adminUser.getId();
		if(adminUser!=null && id!=null) {
			query=adminUserService.updateById(adminUser, id);
		}
		if (query == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
	
	/**
	 * 删除用户信息
	 * param id (用户表主键)
	 * @throws BusinessException 
	 */
	@RequestMapping(value = "deleteById", produces = "application/json;charset=UTF-8")
    @ResponseBody
	public String deleteById(@RequestBody(required = false)String id) throws BusinessException {
		Integer value = null;
		AdminUserQuery query = null;
		if(id!=null) {
			query=JacksonUtils.fromJson(id, AdminUserQuery.class);
			value=adminUserService.deleteById(query.getId());
		}
		if (value == 1){
			return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.SUCCESS, null));
        }
		return JacksonUtils.toJson(this.buildApiResult(APIResultCodeEnum.ERROR, null));
	}
}